home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9701 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  86 lines

  1. Path: newsserver.trl.OZ.AU!cypress!lawsonh
  2. From: lawsonh@melb.cpr.itg.telecom.com.au (Lawson Hanson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Hiding a password
  5. Date: 12 Mar 96 20:59:00 GMT
  6. Organization: Telecom Research Laboratories, Melbourne, Australia.
  7. Message-ID: <lawsonh.826664340@cypress>
  8. References: <1996Feb29.224936.137160@forest>
  9. NNTP-Posting-Host: 144.136.133.19
  10. X-Newsreader: NN version 6.5.0 #2 (NOV)
  11.  
  12. ebromber@forest.drew.edu writes:
  13.  
  14. >I recently wrote a password program. However, there is one little flaw I 
  15. >want to fix. When the password is entered, it is visible on the screen. I 
  16. >was wondering if anyone knows how to hide the password by  printing 
  17. >asterisks instead of the actual character. 
  18. >Thanks in advance.
  19.  
  20. >ebromber@drew.edu
  21.  
  22. Here is an example (it is awful, but works on Unix).
  23. It does not echo "asterisks", just stays blank,
  24. then prints out the password it read:
  25. ------------------------------------
  26. /*
  27. **  Program :  getpass.c
  28. **  Purpose :  Turn off echoing to get a `secret' password
  29. **          :  for use by shell programs, etc.
  30. **    Usage :  PASS=`getpass "Enter password"`
  31. **          :  . . . ${PASS} . . .   # do whatever
  32. **          :  PASS=""               # null out the PASS variable
  33. */
  34. #include <signal.h>
  35. #include <stdio.h>
  36. #include <termios.h>
  37.  
  38.  
  39. extern char *getpass();  /* calls standard `getpass(3)' function */
  40.  
  41.  
  42. int main( argc, argv )
  43.     int   argc;
  44.     char  *argv[];
  45. {
  46.     char  *ptr;
  47.     char  str[BUFSIZ];
  48.  
  49.     str[0] = '\0';
  50.  
  51.     while ( --argc > 0 )
  52.     {
  53.         strcat( str, *++argv );
  54.         strcat( str, " " );
  55.     }
  56.  
  57.     if ( strlen( str ) == 0 )
  58.         strcat( str, "Enter Password: " );
  59.  
  60.     if ( ( ptr = getpass( str ) ) == NULL )
  61.     {
  62.         fprintf( stderr, "getpass: ERROR: returned NULL\n" );
  63.         exit( 1 );
  64.     }
  65.  
  66.     /*  print the entered password
  67.     */
  68.     printf( "%s\n", ptr );
  69.  
  70.     /*  now we could use the password . . .
  71.     */
  72.  
  73.     /*  zero it out when we're done with it
  74.     */
  75.     while ( *ptr != 0 )
  76.         *ptr++ = 0;
  77.  
  78.     exit( 0 );
  79. }
  80. ------------------------------------
  81.  
  82. Best regards,
  83.  
  84. Lawson Hanson
  85.  
  86.